<?php
/**
 * Created by PhpStorm.
 * User: liu85
 * Date: 2018/7/13
 * Time: 23:40
 */

namespace app\admin\controller;


use app\common\consts\BaseConst;
use app\common\helper\AdminInfoHelper;
use app\common\service\ChannelService;
use think\App;

class User extends Auth
{
    private $userModel;

    public function __construct(AdminInfoHelper $adminInfoHelper, App $app = null)
    {
        parent::__construct($adminInfoHelper, $app);
        $this->userModel = new \app\common\model\User();
    }

    /**
     * 获取列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList()
    {
        $map = [];
        $channelId = $this->request->param('channel_id', 0);
        $beginTime = $this->request->param('beginTime', '');
        $endTime = $this->request->param('endTime', '');
        if ($channelId) {
            $map[] = ['channel_id', '=', $channelId];
        }
        if ($beginTime && $endTime) {
            $map[] = ['created', 'between', [strtotime($beginTime . ' 00:00:00'), strtotime($endTime . ' 23:59:59')]];
        }
        $this->assign('beginTime', $beginTime);
        $this->assign('endTime', $endTime);
        $this->assign('channel_id', $channelId);
        $list = $this->userModel->where($map)->order('created desc')->paginate(BaseConst::Admin_DEFAULT_PAGE_SIZE);
        $page = $list->render();
        $list = $list->toArray()['data'];
        $channelIds = array_unique(array_column($list, 'channel_id'));
        $channelList = ChannelService::batchChannelByIds($channelIds);
        foreach ($list as $k => $v) {
            $list[$k]['channel'] = isset($channelList[$v['channel_id']]) ? $channelList[$v['channel_id']]['name'] : '自然流量';
        }
        $count = $this->userModel->where($map)->count();
        $channelMapping = ChannelService::getChannelMapping();
        $this->assign('channelMapping', $channelMapping);
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->assign('count', $count);
        return $this->fetch('user_list');
    }
}